package com.hnisc.controller;

import com.alibaba.fastjson.JSONObject;
import com.hnisc.entity.Flighht;
import com.hnisc.service.CityService;
import com.hnisc.service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 城市控制器
 */
@Controller
public class FlightController {
    @Autowired
    private CityService cityService;
    @Autowired
    private FlightService flightService;

    @GetMapping("/getAll")
    public String getAll(Model model) {
        System.out.println("web层：查询所有城市信息");
        model.addAttribute("city", cityService.getAll());
        return "index";
    }

    @GetMapping("/getCity")
    public String getCity(Model model, HttpServletRequest request) {
        System.out.println("web层：根据起飞城市id和到达城市id查询");
        Integer departureCity = Integer.parseInt(request.getParameter("ago"));
        Integer arrivalCity = Integer.parseInt(request.getParameter("arrive"));
        System.out.println(departureCity);
        System.out.println(arrivalCity);
        List<Flighht> flight = flightService.getCity(departureCity, arrivalCity);
        model.addAttribute("flight",flight);
        model.addAttribute("city", cityService.getAll());
        model.addAttribute("departureCity",departureCity);
        model.addAttribute("arrivalCity",arrivalCity);
        return "index";
    }

    @GetMapping("/getAdd")
    public String getAdd(Model model){
        System.out.println("web层：进入添加首页");
        model.addAttribute("city", cityService.getAll());
        return "add";
    }

    @PostMapping("/getAddData")
    public String getAddData(@RequestParam(value = "flightNo", required = false) String flightNo,
                             @RequestParam(value = "ago", required = false) Integer ago,
                             @RequestParam(value = "arrive", required = false) Integer arrive,
                             HttpServletRequest request,
                             HttpServletResponse response) throws ParseException, IOException {
        System.out.println("web层：添加航班信息");
        String departureTime= request.getParameter("departureTime");
        String arrivalTime = request.getParameter("arrivalTime");
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss");
        Date dTime = formatter.parse(departureTime);
        Date aTime = formatter.parse(arrivalTime);

        Flighht flighht=new Flighht();
        flighht.setFlightNo(flightNo);
        flighht.setDepartureCity(ago);
        flighht.setDepartureTime(dTime);
        flighht.setArrivalCity(arrive);
        flighht.setArrivalTime(aTime);
        Integer count = flightService.getAdd(flighht);
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        if (count == 1){
            out.print("<script>alert('添加成功');window.location='/getAll'</script>");
        }else {
            out.print("<script>alert('添加失败');window.location='/getAdd'</script>");
        }
        out.flush();
        out.close();
        return "add";
    }


    @RequestMapping("/getFlightNo")
    public void getFlightNo(HttpServletRequest request,HttpServletResponse response) throws IOException {
        System.out.println("web层：根据航班编号查询航班记录数");
        String flightNo = request.getParameter("flightNo");
        System.out.println(flightNo);
        Integer count = flightService.getFlightNo(flightNo);
        PrintWriter writer = response.getWriter();
        //Jackson2JsonDecoder.
        writer.write(JSONObject.toJSONString(count));
        writer.close();
    }
}
